package com.xinchao.api;

import com.xinchao.constant.ResponseConstant;
import com.xinchao.exception.ServiceException;
import com.xinchao.model.page.PageInfo;
import com.xinchao.model.sys.JsonResponse;
import com.xinchao.service.AdvertisementMonitorPointService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;

/**
 * @author dxy
 * @date 2018/11/12 14:36
 */
@Controller
@RequestMapping(value = "/point")
public class AdvertisementMonitorPointController {
	private static Logger logger = LoggerFactory.getLogger(AdvertisementMonitorPointController.class);
	@Autowired
	private AdvertisementMonitorPointService advertisementMonitorPointService;
	/**
	 * 获取区域-监播-城市终端总数列表
	 */
	@ResponseBody
	@RequestMapping(value = "/listCityDeviceSum")
	public Object listCityDeviceSum(String advertisementName) {
		//广告名称
		if (StringUtils.isBlank(advertisementName)) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "广告名称不能为空");
		}
		try {
			return advertisementMonitorPointService.listCityDeviceSum(advertisementName);
		} catch (ServiceException e) {
			logger.error("AdvertisementMonitorPointController listCityDeviceSum 获取区域-监播-城市终端总数列表失败：" + e.getMessage());
			return new JsonResponse(ResponseConstant.CODE_FAIL, e.getMessage());
		}
	}

	/**
	 * 获取区域-监播-城市区县终端总数列表
	 */
	@ResponseBody
	@RequestMapping(value = "/listCityRegionDeviceSum")
	public Object listCityRegionDeviceSum(String advertisementName) {
		//广告名称
		if (StringUtils.isBlank(advertisementName)) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "广告名称不能为空");
		}
		try {
			return advertisementMonitorPointService.listCityRegionDeviceSum(advertisementName);
		} catch (ServiceException e) {
			logger.error("AdvertisementMonitorPointController listCityRegionDeviceSum 获取区域-监播-城市区县终端总数列表失败：" + e.getMessage());
			return new JsonResponse(ResponseConstant.CODE_FAIL, e.getMessage());
		}
	}


	/**
	 * 获取点位-监播-曝光度列表
	 */
	@ResponseBody
	@RequestMapping(value = "/listDeviceExposuresSum")
	public Object listDeviceExposuresSum(String advertisementName, Integer timeType) {
		//广告名称
		if (StringUtils.isBlank(advertisementName)) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "广告名称不能为空");
		}
		//时间类型
		if (timeType == null) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "时间类型不能为空");
		}
		try {
			return advertisementMonitorPointService.listDeviceExposuresSumDTO(advertisementName, timeType);
		} catch (ServiceException e) {
			logger.error("AdvertisementMonitorPointController listDeviceExposuresSum 获取点位-监播-曝光度列表失败：" + e.getMessage());
			return new JsonResponse(ResponseConstant.CODE_FAIL, e.getMessage());
		} catch (ParseException e) {
			logger.error("AdvertisementMonitorPointController listDeviceExposuresSum 获取点位-监播-曝光度列表失败：" + e.getMessage());
			return new JsonResponse(ResponseConstant.CODE_FAIL, "系统错误，请联系管理员");
		}
	}

	/**
	 * 获取点位-监播-观看人次列表
	 */
	@ResponseBody
	@RequestMapping(value = "/listDeviceWatchSum")
	public Object listDeviceWatchSum(String advertisementName, Integer timeType) {
		//广告名称
		if (StringUtils.isBlank(advertisementName)) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "广告名称不能为空");
		}
		//时间类型
		if (timeType == null) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "时间类型不能为空");
		}
		try {
			return advertisementMonitorPointService.listDeviceWatchSumDTO(advertisementName, timeType);
		} catch (ServiceException e) {
			logger.error("AdvertisementMonitorPointController listDeviceWatchSum 获取点位-监播-观看人次列表失败：" + e.getMessage());
			return new JsonResponse(ResponseConstant.CODE_FAIL, e.getMessage());
		} catch (ParseException e) {
			logger.error("AdvertisementMonitorPointController listDeviceWatchSum 获取点位-监播-观看人次列表失败：" + e.getMessage());
			return new JsonResponse(ResponseConstant.CODE_FAIL, "系统错误，请联系管理员");
		}
	}

	/**
	 * 获取点位-监播-平均观看时长列表
	 */
	@ResponseBody
	@RequestMapping(value = "/listDeviceAvgWatchSum")
	public Object listDeviceAvgWatchSum(String advertisementName, Integer timeType) {
		//广告名称
		if (StringUtils.isBlank(advertisementName)) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "广告名称不能为空");
		}
		//时间类型
		if (timeType == null) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "时间类型不能为空");
		}
		try {
			return advertisementMonitorPointService.listDeviceAvgWatchSumDTO(advertisementName, timeType);
		} catch (ServiceException e) {
			logger.error("AdvertisementMonitorPointController listDeviceAvgWatchSum 获取点位-监播-平均观看时长列表失败：" + e.getMessage());
			return new JsonResponse(ResponseConstant.CODE_FAIL, e.getMessage());
		} catch (ParseException e) {
			logger.error("AdvertisementMonitorPointController listDeviceAvgWatchSum 获取点位-监播-平均观看时长列表失败：" + e.getMessage());
			return new JsonResponse(ResponseConstant.CODE_FAIL, "系统错误，请联系管理员");
		}
	}

}
